/*
 * Copyright (C) 2012 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.common.util;

import java.io.File;

import android.annotation.TargetApi;
import android.app.DownloadManager;
import android.app.DownloadManager.Query;
import android.app.DownloadManager.Request;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

/**
 * 返利平台工具类
 * 
 * @author andrew.wang
 * @date 2012-7-3
 * 
 */
@TargetApi(9)
public final class CashbackUtils {

  /**
   * 插入一条新的下载任务
   * 
   * @param context
   *          Application Context
   * @param url
   *          下载链接
   * @param title
   *          下载应用名
   * @param pkgName
   *          下载包名
   * @return 下载数据库中的任务ID
   */
  @TargetApi(9)
  public static long insertTask(Context context, String url, String title,
      String pkgName) {

    Request request = new Request(Uri.parse(url));
    request.setTitle(title);
    // 通知栏显示下载进度
    request.setShowRunningNotification(true);
    // 不显示在系统下载管理页面
    request.setVisibleInDownloadsUi(true);
    // 设置应用包名
    request.setDescription(pkgName);
    // 设置MIME类型
    request.setMimeType("application/vnd.android.package-archive");
    // 设置下载路径
    request.setDestinationInExternalPublicDir("/gfan/rebate", title + ".apk");

    DownloadManager manager = (DownloadManager) context
        .getSystemService(Context.DOWNLOAD_SERVICE);
    return manager.enqueue(request);
  }

  /**
   * 利用ID查询任务的状态
   * 
   * @param context
   *          Application Context
   * @param ids
   *          ID数组
   * @return 数据库查询结果
   */
  @TargetApi(9)
  public static Cursor queryWithId(Context context, long... ids) {

    DownloadManager manager = (DownloadManager) context
        .getSystemService(Context.DOWNLOAD_SERVICE);

    return manager.query(new Query().setFilterById(ids));
  }
  
  /**
   * 通过ID安装应用
   * 
   * @param context
   *          Application Context
   * @param id
   *          下载任务ID
   */
  @TargetApi(9)
  public static void install(Context context, long id) {

    DownloadManager manager = (DownloadManager) context
        .getSystemService(Context.DOWNLOAD_SERVICE);

    Cursor c = manager.query(new Query().setFilterById(id));
    if (c.moveToFirst()) {
      String uriString = c.getString(c
          .getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
      String path = Uri.parse(uriString).getPath();
      Utils.D("install path > " + path);
      Utils.installApk(context, new File(path));
    }
  }

}
